using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The TimeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about TimeTable objects.
    /// </summary>
    public class TimeTableDB
    {
        #region "Public Methods"

        public static DataTable GetItemTable(int timeTableKey)
        {
            DataTable myTimeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableSelectSingleItemView", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@timeTableKey", timeTableKey);

                    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
                    myConnection.Open();

                    myTimeTable = new DataTable("tblTimeTable");
                    myAdapter.Fill(myTimeTable);

                    myConnection.Close();
                }
                return myTimeTable;
            }
        }
        /// <summary>
        /// Gets an instance of TimeTable from the underlying datasource.
        /// </summary>
        /// <param name="timeTableKey">The unique TimeTableKey of the TimeTable in the database.</param>
        /// <returns>An TimeTable when the TimeTableKey was found in the database, or null otherwise.</returns>
        public static TimeTable GetItem(int timeTableKey)
        {
            TimeTable myTimeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@timeTableKey", timeTableKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myTimeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myTimeTable;
            }
        }

        /// <summary>
        /// Returns a list with TimeTable objects.
        /// </summary>
        /// <returns>A generics List with the TimeTable objects.</returns>
        public static TimeTableList GetList()
        {
            TimeTableList tempList = new TimeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a TimeTable in the database.
        /// </summary>
        /// <param name="myTimeTable">The TimeTable instance to save.</param>
        /// <returns>The new TimeTableKey if the TimeTable is new in the database or the existing TimeTableKey when an item was updated.</returns>
        public static int Save(TimeTable myTimeTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myTimeTable.TimeTableKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@timeTableKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@timeTableKey", myTimeTable.TimeTableKey);
                    }
                    if (myTimeTable.SchoolKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", myTimeTable.SchoolKey);
                    }
                    if (myTimeTable.MajorKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@majorKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@majorKey", myTimeTable.MajorKey);
                    }
                    if (myTimeTable.StandardLevelKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@standardLevelKey", myTimeTable.StandardLevelKey);
                    }
                    if (myTimeTable.LocationKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@locationKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@locationKey", myTimeTable.LocationKey);
                    }
                    if (myTimeTable.EducationPeriodlKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodlKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodlKey", myTimeTable.EducationPeriodlKey);
                    }
                    if (myTimeTable.DayGroupCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@dayGroupCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@dayGroupCode", myTimeTable.DayGroupCode);
                    }
                    if (myTimeTable.StartTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@startTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@startTime", myTimeTable.StartTime);
                    }

                    if (myTimeTable.SectionDuration == null)
                    {
                        myCommand.Parameters.AddWithValue("@sectionDuration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@sectionDuration", myTimeTable.SectionDuration);
                    }

                    if (myTimeTable.NoOfSection <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@noOfSection", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@noOfSection", myTimeTable.NoOfSection);
                    }

                    if (myTimeTable.NoOfSection < 0)
                    {
                        myCommand.Parameters.AddWithValue("@breakTimeNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTimeNo", myTimeTable.BreakTimeNo);
                    }

                    myCommand.Parameters.AddWithValue("@status", myTimeTable.Status);

                    if (myTimeTable.UserID <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@userID", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userID", myTimeTable.UserID);
                    }

                    if (myTimeTable.BreakTime1Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime1Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime1Duration", myTimeTable.BreakTime1Duration);
                    }

                    if (myTimeTable.BreakTime2Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime2Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime2Duration", myTimeTable.BreakTime2Duration);
                    }

                    if (myTimeTable.BreakTime1Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime3Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime3Duration", myTimeTable.BreakTime3Duration);
                    }

                    if (myTimeTable.BreakTime1Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime4Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime4Duration", myTimeTable.BreakTime4Duration);
                    }

                    if (myTimeTable.BreakTime1Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime5Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime5Duration", myTimeTable.BreakTime5Duration);
                    }

                    if (myTimeTable.BreakTime1Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime6Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime6Duration", myTimeTable.BreakTime6Duration);
                    }

                    if (myTimeTable.BreakTime1Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime7Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime7Duration", myTimeTable.BreakTime7Duration);
                    }

                    if (myTimeTable.BreakTime1Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime8Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime8Duration", myTimeTable.BreakTime8Duration);
                    }

                    if (myTimeTable.BreakTime1Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime9Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime9Duration", myTimeTable.BreakTime9Duration);
                    }

                    if (myTimeTable.BreakTime1Duration == null)
                    {
                        myCommand.Parameters.AddWithValue("@breakTime10Duration", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@breakTime10Duration", myTimeTable.BreakTime10Duration);
                    }

                    //if (myTimeTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myTimeTable.CreatedDateTime);
                    //}
                    //if (myTimeTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myTimeTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a TimeTable from the database.
        /// </summary>
        /// <param name="timeTableKey">The TimeTableKey of the TimeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int timeTableKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTimeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@timeTableKey", timeTableKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the TimeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static TimeTable FillDataRecord(IDataRecord myDataRecord)
        {
            TimeTable myTimeTable = new TimeTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TimeTableKey")))
            {
                myTimeTable.TimeTableKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TimeTableKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                myTimeTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MajorKey")))
            {
                myTimeTable.MajorKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("MajorKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandardLevelKey")))
            {
                myTimeTable.StandardLevelKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StandardLevelKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationKey")))
            {
                myTimeTable.LocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("LocationKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EducationPeriodlKey")))
            {
                myTimeTable.EducationPeriodlKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EducationPeriodlKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DayGroupCode")))
            {
                myTimeTable.DayGroupCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DayGroupCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartTime")))
            {
                //string _startTime = myDataRecord.GetValue(myDataRecord.GetOrdinal("StartTime")).ToString();
                //int _hour = Convert.ToInt32(_startTime.Substring(0, 2));
                //int _min = Convert.ToInt32(_startTime.Substring(3, 2));
                //myTimeTable.StartTime = new TimeSpan(_hour, _min, 0);
                //(myDataRecord.GetOrdinal("StartTime"));
                myTimeTable.StartTime = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("StartTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SectionDuration")))
            {
                //string _strSectionDuration = myDataRecord.GetValue(myDataRecord.GetOrdinal("SectionDuration")).ToString();
                //int _hour = Convert.ToInt32(_strSectionDuration.Substring(0, 2));
                //int _min = Convert.ToInt32(_strSectionDuration.Substring(3, 2));
                //myTimeTable.SectionDuration = new TimeSpan(_hour, _min, 0);
                myTimeTable.SectionDuration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("SectionDuration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("NoOfSection")))
            {
                myTimeTable.NoOfSection = myDataRecord.GetInt16(myDataRecord.GetOrdinal("NoOfSection"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTimeNo")))
            {
                myTimeTable.BreakTimeNo = myDataRecord.GetInt16(myDataRecord.GetOrdinal("BreakTimeNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myTimeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserID")))
            {
                myTimeTable.UserID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserID"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime1Duration")))
            {
                myTimeTable.BreakTime1Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime1Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime1Duration")))
            {
                myTimeTable.BreakTime1Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime1Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime2Duration")))
            {
                myTimeTable.BreakTime2Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime2Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime3Duration")))
            {
                myTimeTable.BreakTime3Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime3Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime4Duration")))
            {
                myTimeTable.BreakTime4Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime4Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime5Duration")))
            {
                myTimeTable.BreakTime5Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime5Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime6Duration")))
            {
                myTimeTable.BreakTime6Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime6Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime7Duration")))
            {
                myTimeTable.BreakTime7Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime7Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime8Duration")))
            {
                myTimeTable.BreakTime8Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime8Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime9Duration")))
            {
                myTimeTable.BreakTime9Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime9Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("BreakTime10Duration")))
            {
                myTimeTable.BreakTime9Duration = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("BreakTime10Duration"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myTimeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myTimeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myTimeTable;
        }
    }

}
